package com.java110.mall.chain.cmd.printer;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.intf.chain.IPrintOrderV1InnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 订单打印
 */
@Java110Cmd(serviceCode = "printer.orderPrinter")
public class OrderPrinterCmd extends Cmd {

    @Autowired
    private IPrintOrderV1InnerServiceSMO printOrderV1InnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        Assert.hasKeyAndValue(reqJson, "shopId", "未包含店铺编号");
        Assert.hasKeyAndValue(reqJson, "orderId", "未包含订单编号");
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        int flag = printOrderV1InnerServiceSMOImpl.printStoreOrder(reqJson.getString("orderId"));
        if (flag < 1) {
            throw new CmdException("打印机配置错误");
        }
        context.setResponseEntity(ResultVo.createResponseEntity(ResultVo.CODE_OK, "打印请求已发送打印机"));

    }
}
